package com.database.db;

import com.alibaba.druid.pool.DruidDataSource;
import com.database.utils.PropertyUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IDEA by Zhoufy on 2018/5/11.
 *
 * @author Zhoufy
 */
public class ConnectionFactory {


    private DruidDataSource datasource;

    private static ConnectionFactory connectionFactory = new ConnectionFactory();


    private ConnectionFactory(){
        datasource = new DruidDataSource();
        String url = PropertyUtils.getValue("jdbcurl");
        String username = PropertyUtils.getValue("username");
        String password = PropertyUtils.getValue("password");
        String driverClassName = PropertyUtils.getValue("driverClassName");
        getDruidDataSource(username, password, url, driverClassName);
    }

    public static ConnectionFactory getInstance() {
        return connectionFactory;
    }


    public DruidDataSource getDruidDataSource(String username, String password, String jdbcURL, String driverClassName) {


//        int initialSize = Integer.parseInt(PropertyUtils.getValue("datasource.initialSize"));
//        int minIdle = Integer.parseInt(PropertyUtils.getValue("datasource.minIdle"));
//        int maxActive = Integer.parseInt(PropertyUtils.getValue("datasource.maxActive"));
//        int maxWait = Integer.parseInt(PropertyUtils.getValue("datasource.maxWait"));
//        int timeBetweenEvictionRunsMillis = Integer.parseInt(PropertyUtils.getValue("datasource.timeBetweenEvictionRunsMillis"));
//        int minEvictableIdleTimeMillis = Integer.parseInt(PropertyUtils.getValue("datasource.minEvictableIdleTimeMillis"));
//        String validationQuery = PropertyUtils.getValue("datasource.validationQuery");
//        boolean testWhileIdle = Boolean.parseBoolean(PropertyUtils.getValue("datasource.testWhileIdle"));
//        boolean testOnBorrow = Boolean.parseBoolean(PropertyUtils.getValue("datasource.testOnBorrow"));
//        boolean testOnReturn = Boolean.parseBoolean(PropertyUtils.getValue("datasource.testOnReturn"));
//        int maxOpenPreparedStatements = Integer.parseInt(PropertyUtils.getValue("datasource.maxOpenPreparedStatements"));
//        boolean removeAbandoned = Boolean.parseBoolean(PropertyUtils.getValue("datasource.removeAbandoned"));
//        int removeAbandonedTimeout = Integer.parseInt(PropertyUtils.getValue("datasource.removeAbandonedTimeout"));
//        boolean logAbandoned = Boolean.parseBoolean(PropertyUtils.getValue("datasource.logAbandoned"));

//        datasource.setDriverClassName(driverClassName);
//        datasource.setInitialSize(initialSize);
//        datasource.setMinIdle(minIdle);
//        datasource.setMaxActive(maxActive);
//        datasource.setMaxWait(maxWait);
//        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
//        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
//        datasource.setValidationQuery(validationQuery);
//        datasource.setTestWhileIdle(testWhileIdle);
//        datasource.setTestOnBorrow(testOnBorrow);
//        datasource.setTestOnReturn(testOnReturn);
//        datasource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
//        datasource.setRemoveAbandoned(removeAbandoned);
//        datasource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
//        datasource.setLogAbandoned(logAbandoned);

        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setUrl(jdbcURL);
        datasource.setDriverClassName(driverClassName);
        return datasource;
    }




    public Connection getConnection() throws SQLException {
        return datasource.getConnection();
    }



    /**
     * 关闭数据库连接
     *
     * @param connection
     * @param prepareStatement
     * @param resultSet
     */
    public void closeConnection(Connection connection, PreparedStatement prepareStatement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (prepareStatement != null) {
                prepareStatement.close();
            }
//            if (connection != null) {
//                connection.close();
//            }
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "code = " + e.getErrorCode());
        }
    }
}
